package com.my.swas.sso.module.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Splitter;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.module.admin.entity.User;
import com.my.swas.sso.module.admin.dto.UserDto;
import com.my.swas.sso.module.admin.service.UserService;
import com.my.swas.sso.module.admin.vo.UserVo;
import jodd.util.BCrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description:
 * @author: :MaYong
 */
@Slf4j
@Controller
@Transactional
@RequestMapping("admin/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping(value = "userList")
    @ResponseBody
    public IPage<UserVo> getUserList(UserDto userDto) {
        return userService.pageList(userDto);
    }

    /**
     * 打开新增用户页面
     *
     * @return
     */
    @PostMapping("showAddPage")
    public String saveUser() {
        return "layui/system/user/add";
    }


    @PostMapping("save")
    @ResponseBody
    public ResponseResult saveUser(UserDto userDto) {
        User user = new User();
        BeanCopierUtil.convert(userDto, user);
        Long userId = userDto.getId();
        if (null == userId) {
            user.setPassword(BCrypt.hashpw(userDto.getFirstPassword(), BCrypt.gensalt()));
            userService.save(user);
        } else {
            userService.updateById(user);
        }
        return ResponseResult.ok;
    }


    @GetMapping("remove")
    @ResponseBody
    public ResponseResult removeUser(String ids) {
        userService.removeByIds(Splitter.on(",").splitToList(ids));
        return ResponseResult.ok;
    }

    /**
     * 跳转编辑用户页面
     *
     * @param id
     * @return
     */
    @GetMapping("toEditPage")
    public String toEditPage(Long id, Model model, @RequestParam(value = "type", required = false) String type) {
        User user = new User();
        user.setId(id);
        User dbUser = userService.getOne(new QueryWrapper<>(user));
        model.addAttribute("user", dbUser);
        if (StringUtils.isEmpty(type)) {
            return "layui/system/user/edit";
        } else {
            return "layui/system/user/detail";
        }
    }


    /**
     * 跳转角色分配页面
     *
     * @param userId
     * @return
     */
    @GetMapping("toGrantRolesPage")
    public String toGrantRolesPage(Long userId, Model model) {
        User user = new User();
        user.setId(userId);
        User dbUser = userService.getOne(new QueryWrapper<>(user));
        model.addAttribute("user", dbUser);
        return "layui/system/user/grant";
    }


    @PostMapping("grantRoles")
    @ResponseBody
    public ResponseResult grantRoles(Long userId, @RequestParam(required = false) String roleIds) {
        userService.grantRoles(userId, roleIds);
        return ResponseResult.ok;
    }

    @GetMapping("userSwitch")
    @ResponseBody
    public ResponseResult userSwitch(Integer enable, Long userId) {
        User user = new User();
        user.setId(userId);
        user.setEnable(enable);
        userService.updateById(user);
        return ResponseResult.ok;
    }

}
